<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>
    <div class="head-title">组件样式</div>

    <div class="component-style">
      <el-form :model="form">
        <!--        <el-form-item label="排列方式：">-->
        <!--          <el-radio-group v-model="showComponentData.layout">-->
        <!--            <el-radio value="single">单视频</el-radio>-->
        <!--            <el-radio value="carousel">轮播图</el-radio>-->
        <!--            <el-radio value="grid-2">一行两个</el-radio>-->
        <!--            <el-radio value="horizontal-scroll">一行三个</el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->
        <el-form-item label="标题栏显示：">
          <el-radio-group v-model="showComponentData.show_title">
            <el-radio :value="true">显示</el-radio>
            <el-radio :value="false">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标题名称：" v-if="showComponentData.show_title">
          <el-input v-model="showComponentData.title" />
        </el-form-item>
        <el-form-item label="文字大小：" v-if="showComponentData.show_title">
          <div class="flex">
            <el-slider style="width: 70%" v-model="form.itemStyle.fontSize" :min="12" :max="30" />
            <el-input
              type="number"
              style="width: 30%; margin-left: 20px"
              v-model.number="form.itemStyle.fontSize"
              :min="12"
              :max="30"
            >
              <template #append>px</template>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item label="字体颜色：" v-if="showComponentData.show_title">
          <el-color-picker v-model="form.itemStyle.color"></el-color-picker>
          <el-input style="width: 160px; margin-left: 10px" v-model="form.itemStyle.color">
            <template #append>
              <el-button @click="form.itemStyle.color = '#333333'">重置</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="查看更多：">
          <el-radio-group v-model="showComponentData.show_more">
            <el-radio :value="true">显示</el-radio>
            <el-radio :value="false">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="字体名称" v-if="showComponentData.show_more">
          <el-input v-model="showComponentData.show_more_text"></el-input>
        </el-form-item>
        <el-form-item label="跳转链接" v-if="showComponentData.show_more">
          <el-link type="primary" @click="showLinkDataDialog = true">请选择跳转链接</el-link>
        </el-form-item>
        <!--        <el-form-item label="视频标题显示">-->
        <!--          <el-radio-group v-model="showComponentData.text_layout">-->
        <!--            <el-radio value="none">不显示</el-radio>-->
        <!--            <el-radio value="inside">视频内部</el-radio>-->
        <!--            <el-radio value="outside">视频外部</el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->
      </el-form>
    </div>

    <div class="add-area">
      <div class="add-title">视频内容</div>
      <VueDraggable
        v-model="showComponentData.default_sub_list"
        :animation="150"
        ghostClass="ghost"
        :filter="'.el-input'"
        :preventOnFilter="false"
        class="content"
        @end="handleUpdateComponent"
      >
        <div
          v-for="(item, index) in showComponentData.default_sub_list"
          :key="item.id"
          class="carousel-item"
        >
<!--          <div class="video-title">-->
<!--            <el-form-item label="视频标题：">-->
<!--              <el-input v-model="item.fileName"></el-input>-->
<!--            </el-form-item>-->
<!--          </div>-->
          <div class="video-poster">
            <el-form-item label="视频封面：">
              <div class="avatar-uploader" @click="handleChangeImage(index)">
                <el-image
                  v-if="item.poster"
                  :src="item.poster"
                  class="avatar"
                  fit="cover"
                ></el-image>
                <i v-else class="el-icon-plus avatar-uploader-icon"
                  ><el-icon> <Plus /> </el-icon
                ></i>
                <div v-if="item.poster" class="mask">替换</div>
              </div>
            </el-form-item>
          </div>
          <div class="video-content">
            <el-form-item label="视频地址：">
              <el-input v-model="item.fileUrl">
                <template #append
                  ><el-button @click="changeVideo(index)">选择视频</el-button></template
                >
              </el-input>
            </el-form-item>
          </div>
          <!--          <div class="link-area">-->
          <!--            <div>跳转链接:</div>-->
          <!--            <div class="link">-->
          <!--              <el-icon style="color: var(&#45;&#45;el-color-primary)"><Link /></el-icon>-->
          <!--              <el-link type="primary" @click="handleOpenLinkDialog(index)">{{-->
          <!--                item.link?.name || '请选择跳转链接'-->
          <!--              }}</el-link></div-->
          <!--            >-->
          <!--          </div>-->
          <!--          <el-icon class="del-icon" :size="16" color="red" @click="handleDel(index)"-->
          <!--            ><CircleCloseFilled-->
          <!--          /></el-icon>-->
        </div>

        <div>
          <!--          <el-button type="primary" @click="handleAdd" plain :icon="Plus" style="width: 100%"-->
          <!--            >添加视频</el-button-->
          <!--          >-->
          <el-tag
            type="primary"
            class="submit-btn"
            @click="handleAdd"
            v-if="showComponentData.layout !== 'single'"
            >添加视频</el-tag
          >
        </div>
      </VueDraggable>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

    <video-group-dialog
      v-if="showVideoListDialog"
      :single-type="selectVideoSingle"
      :operation-type="false"
      :show-video-choose-dialog="showVideoListDialog"
      @close-dialog="handleCloseDialog"
    ></video-group-dialog>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      @close-dialog="handleCloseLinkDialog"
    ></link-list-dialog>

    <image-selected-dialog
      v-if="showImageChooseDialog"
      :single-type="true"
      :operation-type="false"
      :show-image-choose-dialog="showImageChooseDialog"
      @close-dialog="handleCloseImageDialog"
    ></image-selected-dialog>
  </div>
</template>

<script setup lang="ts">
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import VideoGroupDialog from '@/views/decoration/diyComponents/VideoGroupDialog.vue'
  import { VueDraggable } from 'vue-draggable-plus'
  import { Plus } from '@element-plus/icons-vue'
  import debounce from 'lodash/debounce'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'
  import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'

  const props = defineProps({
    contentData: {
      type: Object,
      default() {
        return {}
      }
    }
  })

  const form = reactive({
    itemStyle: {
      fontSize: 18,
      color: '#333',
      fontWeight: 600,
      height: 100
    }
  })

  const showLinkDataDialog = ref(false)
  const selectVideoSingle = ref(false)
  const showImageChooseDialog = ref(false)
  const changeIndex = ref()

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.itemStyle = raw.itemStyle
    handleUpdateComponent()
  }, 300)

  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)
  const showVideoListDialog = ref(false)

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function handleAdd() {
    showVideoListDialog.value = true
    selectVideoSingle.value = false
  }

  function changeVideo(index: number) {
    showVideoListDialog.value = true
    selectVideoSingle.value = true
    changeIndex.value = index
  }

  // function handleDel(index: number) {
  //   showComponentData.value.default_sub_list.splice(index, 1)
  //   handleUpdateComponent()
  // }

  function handleChangeImage(index: number) {
    changeIndex.value = index
    showImageChooseDialog.value = true
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  // function handleOpenLinkDialog(index: number) {
  //   showLinkDataDialog.value = true
  //   changeIndex.value = index
  // }

  function handleCloseLinkDialog(value: any) {
    if (value) {
      showComponentData.value.default_sub_list[changeIndex.value].link = value
    }
    showLinkDataDialog.value = false
  }

  function handleCloseDialog(value: any) {
    if (value) {
      if (Array.isArray(value)) {
        showComponentData.value.default_sub_list.push(...value)
      } else {
        showComponentData.value.default_sub_list[changeIndex.value] = value
      }
      handleUpdateComponent()
    }
    showVideoListDialog.value = false
  }

  function handleCloseImageDialog(value: any) {
    if (value) {
      if (Array.isArray(value)) {
        showComponentData.value.default_sub_list.push(...value)
      } else {
        showComponentData.value.default_sub_list[changeIndex.value].poster = value.fileUrl
      }
      handleUpdateComponent()
    }
    showImageChooseDialog.value = false
  }

  onMounted(() => {
    if (showComponentData.value.itemStyle.fontSize) {
      form.itemStyle.fontSize = showComponentData.value.itemStyle.fontSize
      form.itemStyle.color = showComponentData.value.itemStyle.color
      form.itemStyle.fontWeight = showComponentData.value.itemStyle.fontWeight
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .head-title {
      padding: 20px 0 0 0;
      font-size: 14px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .add-area {
      width: 100%;

      .add-title {
        font-size: 14px;
        color: black;
        font-weight: bold;
        margin-bottom: 20px;
      }

      .content {
        .carousel-item {
          width: 100%;
          margin-bottom: 20px;
          position: relative;
          display: flex;
          flex-direction: column;
          background-color: #f7f8fa;
          padding: 10px;
          box-sizing: border-box;

          &:hover {
            .del-icon {
              display: block;
            }
          }

          .video-title {
            margin-bottom: 20px;
          }

          .video-poster {
            .avatar-uploader {
              border: 1px dashed #d9d9d9;
              border-radius: 12px;
              cursor: pointer;
              position: relative;
              overflow: hidden;
              width: 80px;
              height: 80px;
              background-color: #fff;
              text-align: center;

              .mask {
                position: absolute;
                bottom: 0;
                left: 0;
                width: 100%;
                height: 24px;
                background-color: rgba(0, 0, 0, 0.6);
                display: flex;
                align-items: center;
                justify-content: center;
                opacity: 0;
                transition: opacity 0.3s ease;
                color: #fff;
                font-size: 12px;
              }

              &:hover .mask {
                opacity: 1;
              }
            }

            .avatar {
              width: 100%;
              height: 80px;
              display: block;
            }
          }

          .link-area {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-top: 20px;
            font-size: 14px;
          }

          .link {
            width: 80%;
            display: flex;
            align-items: center;
            height: 40px;
            padding-left: 10px;
            background-color: #fff;
          }

          .del-icon {
            display: none;
            position: absolute;
            top: -2px;
            right: -2px;
            cursor: pointer;
            color: #8c939d;
          }
        }
      }
    }

    .component-style {
      width: 100%;
      box-sizing: border-box;
      margin-top: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .title {
          font-size: 16px;
          font-weight: bold;
        }
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }
    }

    .submit-btn {
      width: 100%;
      height: 40px !important;
      cursor: pointer;
      font-size: 14px;
    }
  }
</style>
